import Vue from "vue";
import Vuex from "vuex"

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        num: 2
    },
    getters: {
        addNum(state) {
            return state.num * 2
        }
    },
    // 修改state数据的唯一来源
    mutations: {
        //每次加1
        increment(state) {
            state.num++;
        },

        //每次加n
        incrementN(state, { n }) {
            state.num += n;
        },
    },
    actions: {
        incrementWait({ commit }) {
            setTimeout(() => {
                commit("increment");
            }, 1000);
        },

        incrementNWait({ commit }, payload) {
            setTimeout(() => {
                commit("incrementN", payload);
            }, 1000);
        },
    }
})

export default store